<?php

namespace App\ModelFilters;

use Carbon\Carbon;
use EloquentFilter\ModelFilter;

class QHSchoolArticleFilter extends ModelFilter
{
    /**
    * Related Models that have ModelFilters as well as the method on the ModelFilter
    * As [relationMethod => [input_key1, input_key2]].
    *
    * @var array
    */
    public $relations = [];

    public function province($v) {
        return $this->whereRaw('province = ? OR province IS NULL', [$v]);
    }

    public function schoolId($v) {
        return $this->where('schoolId', $v);
    }

    public function keyword($v) {
        return $this->where('title', 'like', '%'.$v.'%')->orWhere('source', 'like', '%'.$v.'%')->orWhere('content', 'like', '%'.$v.'%');
    }

    public function type($v) {
        return $this->where('type', $v);
    }

    public function key($v) {
        return $this->where('title', 'like', '%'.$v.'%')
            ->orWhere('source', 'like', '%'.$v.'%')
            ->orWhereHas('school', function ($q) use ($v) {
                $q->where('name', 'like', '%'.$v.'%');
            });
    }

    public function year($v) {
        return $this->where('title', 'like', '%'.$v.'%')->orWhereYear('created_at', $v);
    }

    public function id($v) {
        return $this->where('schoolId', $v);
    }
}
